package com.jushu.storbox.callback;

import java.util.Comparator;

import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;

public class FileFolderInfoTypeSort implements Comparator<FileFolderInfo> {
	private int sortType ;
	public  FileFolderInfoTypeSort(int sortType) {//0x0001 0x1001
		this.sortType = sortType;
	}
	@Override
	public int compare(FileFolderInfo lhs, FileFolderInfo rhs) {
		int compareAscResult = 1;
		compareAscResult = compareFileFolderInfo(lhs, rhs);
		if(compareAscResult == 0) {
			compareAscResult = lhs.getName().compareTo(rhs.getName());
			return modifyResult(compareAscResult, true);
		}
		return modifyResult(compareAscResult, false);
	}
	
	private int modifyResult(int result, boolean modifyFlag) {
		if(!modifyFlag) {
			return result;
		} else {
			if(sortType == UiConstant.SORT_BY_NAME) {//升序排序
				return result;
			} else {
				return (result * -1);
			}
		}
	}
	
	private int compareFileFolderInfo(FileFolderInfo lhs, FileFolderInfo rhs) {
		if (lhs.getIsFile() == FileType.FOLDER_TYPE_VALUE 
				&& rhs.getIsFile() == FileType.FILE_TYPE_VALUE) {
			return -1;
		} else if (lhs.getIsFile() == FileType.FILE_TYPE_VALUE 
				&& rhs.getIsFile() == FileType.FOLDER_TYPE_VALUE) {
			return 1;
		} else {
			return 0;
		}
	}
}

